# -*- coding: utf8 -*-

import logging
import logging.handlers
import time
import request_cic.cic_settings as SE
rq = time.strftime('%Y%m%d',time.localtime(time.time()))
#
class Logger(logging.Logger):
    def __init__(self, filename=None):
        super(Logger, self).__init__(self)
        # 日志文件名
        if filename is None:
            filename = SE.logpath + rq + '.log'
        self.filename = filename

        # 创建一个handler，用于写入日志文件 (每天生成1个，保留10天的日志)
        fh = logging.handlers.TimedRotatingFileHandler(self.filename, 'D', 1, 10)
        fh.suffix = "%Y%m%d-%H%M.log"
        fh.setLevel(logging.DEBUG)


        # 再创建一个handler，用于输出到控制台
        ch = logging.StreamHandler()
        #ch.setLevel(logging.WARNING)
        ch.setLevel(logging.INFO)

        # 定义handler的输出格式
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s[line:%(lineno)d] - %(message)s')
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)

        # 给logger添加handler
        self.addHandler(fh)
        self.addHandler(ch)


if __name__ == '__main__':
    pass